//
//  MHBaseChangeViewController.m
//  ManHua
//
//  Created by 江子安 on 15/11/3.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHBaseChangeViewController.h"
#import <AFNetworking.h>
#import "MHDataManager.h"

@interface MHBaseChangeViewController ()

@property (nonatomic, strong) UIBarButtonItem *saveBtn;

@end

@implementation MHBaseChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStyleDone) target:self action:@selector(didClickSaveInfo)];
    self.navigationItem.rightBarButtonItem = self.saveBtn;
    self.view.backgroundColor = [UIColor whiteColor];
    
    _birthday = self.userInfo.UserBorthday;
    _nick = self.userInfo.UserNick;
    _from = self.userInfo.UserForm;
    _sign = self.userInfo.UserOther;
    
}

- (void)didClickSaveInfo
{
    self.userInfo.UserBorthday = _birthday;
    self.userInfo.UserNick = _nick;
    self.userInfo.UserForm = _from;
    self.userInfo.UserOther = _sign;
    NSString *nick = [self.userInfo.UserNick stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *from = [self.userInfo.UserForm stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *userid = [NSString stringWithFormat:@"%ld", self.userInfo.UserID];
    NSString *birthday = self.userInfo.UserBorthday;
    NSString *other = [self.userInfo.UserOther stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *sex = [self.userInfo.UserSex stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *pathString = [NSString stringWithFormat:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=changinfo&nick=%@&from=%@&userid=%@&birthday=%@&other=%@&sex=%@", nick, from, userid, birthday, other, sex];
    NSURLRequest *requset = [NSURLRequest requestWithURL:[NSURL URLWithString: pathString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError != nil) {
            NSLog(@"%@", connectionError.localizedDescription);
            return;
        }
        NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        if ([result isEqualToString:@"1"]) {
            NSLog(@"修改成功");
            self.passInfoBlock(self.userInfo);
            [self.navigationController popViewControllerAnimated:YES];
            [[MHDataManager shareManager] deleteUserInfo];
            [[MHDataManager shareManager] insertUserInfo:self.userInfo];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"logout" object:nil];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
